package com.exam.controller;

import com.exam.service.QuestionService;
import com.exam.utils.ResultVOUtil;
import com.exam.vo.ResultVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by v
 * Date:2018/12/6
 * Time:19:10
 */
@RestController
@RequestMapping("/admin/question")
@Slf4j
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 新增题目
     * @param map
     * @return
     */
    @PostMapping("/insert")
    public ResultVO insertQuestion(@RequestBody Map<String, Object> map){

        questionService.insertQuestion(map);
        return ResultVOUtil.success();
    }

    /**
     * 信息修改
     * @return
     */
    @PostMapping("/modify")
    public ResultVO modifyQuestion(@RequestBody Map<String, Object> questionModify) {

        questionService.modifyQuestion(questionModify);
        return ResultVOUtil.success();
    }

    /**
     * 题目列表
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResultVO getallQuestionList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "size", defaultValue = "10") Integer size) {

//        Integer userId = RedisUserIdUtil.getAdminUserId(request,redisTemplate);
        PageInfo<Map<String, Object>> all = questionService.getQuestionList(page, size);

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("number", all.getTotal());
        map.put("currentPage", page);
        map.put("data", all.getList());
        return ResultVO.success(map);
    }

}
